﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using DatabaseHandler; //referencja do DatabaseHandler
using System.Web.Services.Protocols;

namespace Server
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {
        public AuthentificationCredentials Authentification;

        [WebMethod (Description = "Używane podczas rejestracji")]
        public void RegisterUser(User _user)
        {
            DatabaseWarehouseHandler.instance.RegisterUser(_user);
        }

        [WebMethod (Description = "Używane w panelu admina")]
        [SoapHeader ("Authentification")]
        public void AddUser(User _user)
        {
            if (Authentification.Privileges.Equals("admin"))
            {
                DatabaseWarehouseHandler.instance.AddUser(_user);
            }
        }

        [WebMethod]
        [SoapHeader("Authentification")]
        public void DeleteUser(User _user)
        {
            if (Authentification.Privileges.Equals("admin"))
            {
                DatabaseWarehouseHandler.instance.DeleteUser(_user);
            }
        }

        [WebMethod]
        [SoapHeader("Authentification")]
        public void EditUser(User _user)
        {
            if (Authentification.Privileges.Equals("admin"))
            {
                DatabaseWarehouseHandler.instance.EditUser(_user);
            }
        }

        [WebMethod]
        public bool CheckUserExist(User _user)
        {
            return DatabaseWarehouseHandler.instance.CheckUserExist(_user);
        }

        [WebMethod]
        public bool CheckUserPass(User _user)
        {
            return DatabaseWarehouseHandler.instance.CheckUserPass(_user);
        }

        [WebMethod]
        public bool CheckIfAdmin(User _user)
        {
            return DatabaseWarehouseHandler.instance.CheckIfAdmin(_user);
        }

        [WebMethod]
        public List<User> GetAllUsers()
        {
            return DatabaseWarehouseHandler.instance.GetAllUsers();
        }

        [WebMethod]
        public List<User> GetUsersByPrivileges(User _user)
        {
            return DatabaseWarehouseHandler.instance.GetUsersByPrivileges(_user);
        }

        [WebMethod]
        public bool CheckProductExist(Product _product)
        {
            return DatabaseWarehouseHandler.instance.CheckProductExist(_product);
        }

        [WebMethod]
        [SoapHeader("Authentification")]
        public void DeleteProduct(Product _product)
        {
            if (Authentification.Privileges.Equals("admin"))
            {
                DatabaseWarehouseHandler.instance.DeleteProduct(_product);
            }
        }

        [WebMethod]
        [SoapHeader("Authentification")]
        public void AddProduct(Product _product)
        {
            if (Authentification.Privileges.Equals("admin"))
            {
                DatabaseWarehouseHandler.instance.AddProduct(_product);
            }
        }

        [WebMethod]
        public List<Product> GetAllProducts()
        {
            return DatabaseWarehouseHandler.instance.GetAllProducts();
        }

        [WebMethod]
        public List<Product> GetProductsByCategory(Product _product)
        {
            return DatabaseWarehouseHandler.instance.GetProductsByCategory(_product);
        }

        [WebMethod]
        [SoapHeader("Authentification")]
        public void EditProduct(Product _product)
        {
            if (Authentification.Privileges.Equals("admin"))
            {
                DatabaseWarehouseHandler.instance.EditProduct(_product);
            }
        }

        [WebMethod]
        [SoapHeader("Authentification")]
        public void BuyProduct(Product _product)
        {
            if (Authentification.Privileges.Equals("user"))
            {
                DatabaseWarehouseHandler.instance.BuyProduct(_product);
            }
        }

        [WebMethod]
        public double GetPrice(Product _product)
        {
            return DatabaseWarehouseHandler.instance.GetPrice(_product);
        }

        [WebMethod]
        public bool CheckProductQuantity(Product _product)
        {
            return DatabaseWarehouseHandler.instance.CheckProductQuantity(_product);
        }
    }
}